// Dart imports:
import 'dart:io';

// Package imports:
import 'package:drift/drift.dart';
import 'package:drift/native.dart';

// Project imports:
import 'package:palmf_hid/common/entities/data_base/my_plamf_entity.dart';
import 'package:palmf_hid/common/utils/database_backup_util/database_backup_util.dart';
import 'package:palmf_hid/common/utils/my_log_util.dart';

part 'my_database.g.dart';
//flutter pub run build_runner build

@DriftDatabase(tables: [ResultEntity, MyPalmFEntity,ProjectParamsEntity,PersonalEntity,PetInfoEntity])
class MyDatabase extends _$MyDatabase {
  MyDatabase() : super(_openDatabase());

  @override
  int get schemaVersion => 2;

  //数据库迁移方法
  @override
  MigrationStrategy get migration {
    return MigrationStrategy(
        beforeOpen: (details) async {},
        onUpgrade: (Migrator m, int from, int to) async {
          if (from < 3) {
            // we added the dueDate property in the change from version 1 to
            // version 2
           m.createTable(projectParamsEntity);
           MyLogUtil().i("创建MigrationStrategy");
          }
        });
  }
}

//自定义打开数据库的方法
QueryExecutor _openDatabase() {
  return LazyDatabase(() async {
    //这是数据库文件。
    final dbFile = await DatabaseBackupUtil.dbPath;
    MyLogUtil().i('数据库路径：$dbFile');
    return NativeDatabase(
      File(dbFile),
    );
  });
}
